!function () {
    function InterceptorManager() {
        this.handlers = []
    }
    InterceptorManager.prototype.use = function (fulfilled, rejected) {
        this.handlers.push({
            fulfilled,
            rejected
        })
    }


    function Axios(config) {
        this.defaults = config;
        this.interceptors = {
            response: new InterceptorManager(),
            request: new InterceptorManager(),
        }
    }


    Axios.prototype.request = function (config) {
        // 真正发送请求的函数
        const dispatchRequst = function () {
            return new Promise((resolve, reject) => {
                const xhr = new XMLHttpRequest()
                xhr.open(config.method, config.url);
                xhr.send()
                xhr.onreadystatechange = function () {
                    if (xhr.readyState === 4) {
                        if (/^2\d{2}$/.test(xhr.status)) {
                            resolve({
                                data: xhr.response,
                                status: xhr.status,
                                config: config,
                                headers: {},
                                request: xhr
                            })
                        } else {
                            reject(xhr.status);
                        }
                    }
                }

                if (config.cancelToken) {
                    config.cancelToken.promise.then(() => {
                        console.log('abort'); d
                        xhr.abort();
                    })
                }
            })
        }
        const chain = [dispatchRequst, undefined];
        // 请求拦截器添加成功
        this.interceptors.request.handlers.forEach(item => {
            chain.unshift(item.fulfilled, item.rejected);
        })
        // 相应拦截器
        this.interceptors.response.handlers.forEach(item => {
            chain.push(item.fulfilled, item.rejected);
        })
        console.log('chain: ', chain);
        // 创建一个成功的promise激活chain链条
        let promise = Promise.resolve();
        while (chain.length) {
            promise = promise.then(chain.shift(), chain.shift())
        }
        return promise;
    }
    const methods = ['put', 'post', 'delete', 'get', 'patch']
    methods.forEach(methodName => {
        Axios.prototype[methodName] = function () {
            // 模拟Axios结构，没有方法具体实现
        }
    })
    console.dir(Axios);

    /**
     * 创建 axios
     * axios的身份是request函数,函数中的this，指向Axios的实例对象，因为要使用interceptors
     * 
     */
    function createInstance(config) {
        // 创建一个Axios的实例对象
        const context = new Axios(config);// defaults interceptors
        // axios 是一个函数，axios({})发请求
        const instance = Axios.prototype.request.bind(context);
        // 给他添加上属性和方法
        // console.log(Object.keys(Axios.prototype));
        // Object.keys(Axios.prototype)
        // 将Axios.prototype原型上的方法，都添加给instance
        Object.keys(Axios.prototype).forEach(methodName => {
            // console.log(methodName);
            instance[methodName] = Axios.prototype[methodName]
        })
        // 将实例对象身上的defaults和拦截器也给instance
        Object.keys(context).forEach(key => {
            instance[key] = context[key]
        })
        // console.dir(instance);
        return instance;
    }
    function CancelToken(executor) {
        let outFn = null;
        this.promise = new Promise((resolve, reject) => {
            outFn = resolve;
        })
        executor(() => {
            console.log('123123');
            outFn();
        })
    }

    const axios = createInstance({})
    axios.Axios = Axios;

    axios.create = function (config) {
        return createInstance(config)
    }

    axios.CancelToken = CancelToken

    // all 
    axios.all = function(promiseArr){
        return Promise.all(promiseArr);
    }
    window.axios = axios;
}()